package ucn.dmf83.sc.workshop1.group3.DomainLayer;

import java.util.ArrayList;
import java.util.Iterator;

public class AddressBook {
	private ArrayList<Copy> copies;
	private ArrayList<DVD> dvds;
	private ArrayList<Person> people;

	public AddressBook() {
		people = new ArrayList<Person>();
		dvds = new ArrayList<DVD>();
		copies = new ArrayList<Copy>();
	}

	public void addCopy(DVD dvd, String serialNum, String purchaseDate,
			double purchasePrice) {
		copies.add(new Copy(dvd, serialNum, purchaseDate, purchasePrice));
	}

	public void addDVD(DVD dvd) {
		dvds.add(dvd);
	}

	public void addDVD(String title, String publishDate) {
		dvds.add(new DVD(dvds.size() + 1, title, publishDate));
	}

	public void addLoan(Person person, String borrowDate, int period, Copy copy) {
		person.addLoan(borrowDate, period, copy);
	}

	public void addPerson(Person person) {
		people.add(person);
	}

	public void addPerson(String name, String address, String city,
			int postCode, String phone) {
		int id = people.size();
		Person person = new Person(id, name, address, postCode, city, phone);
		this.addPerson(person);
	}

	public ArrayList<Copy> getCopies(DVD dvd) {
		ArrayList<Copy> returnCopies = new ArrayList<Copy>();

		for (Iterator<Copy> it = copies.iterator(); it.hasNext();) {
			Copy copy = it.next();
			if ( copy.getOriginal().equals(dvd))
				returnCopies.add(copy);
		}

		return returnCopies;
	}

	public ArrayList<Copy> getCopies(int id) {
		return getCopies(this.getDVD(id));
	}

	public ArrayList<Copy> getCopies(String title) {
		return getCopies(this.getDVD(title));
	}

	public Copy getCopy(String serialNum) {
		Copy copy = new Copy();
		for (Iterator<Copy> it = copies.iterator(); it.hasNext();) {
			Copy temp = it.next();
			if (temp.getSerialNum().equals(serialNum))
				copy = temp;
		}
		return copy;
	}

	public DVD getDVD(int id) {
		DVD returndvd = new DVD();
		for (Iterator<DVD> it = dvds.iterator(); it.hasNext();) {
			DVD temp = it.next();
			if (temp.getId() == id)
				returndvd = temp;
		}
		return returndvd;
	}

	public DVD getDVD(String title) {
		DVD returndvd = new DVD();
		for (Iterator<DVD> it = dvds.iterator(); it.hasNext();) {
			DVD temp = it.next();
			if (temp.getTitle().equals(title))
				returndvd = temp;
		}
		return returndvd;
	}
	
	public ArrayList<DVD> getDVDs(){
		return this.dvds;
	}
	
	public ArrayList<DVD> getDVDs(String partialTitle) {
		ArrayList<DVD> returnDVD = new ArrayList<DVD>();
		
		for(Iterator<DVD> it = this.dvds.iterator(); it.hasNext();) {
			DVD temp = it.next();
			if(temp.getTitle().contains(partialTitle))
				returnDVD.add(temp);
		}
		return returnDVD;
	}

	public ArrayList<Loan> getLoans() {
		ArrayList<Loan> tempList = new ArrayList<Loan>();

		for (Iterator<Person> it = people.iterator(); it.hasNext();) {
			for (Iterator<Loan> ita = it.next().getLoans().iterator(); it.hasNext();) {
				tempList.add(ita.next());
			}
		}

		return tempList;
	}

	public ArrayList<Loan> getLoans(Person person) {
		ArrayList<Loan> tempList = new ArrayList<Loan>();
		for (Iterator<Person> it = people.iterator(); it.hasNext();) {
			if (it.next().equals(person)) {
				for (Iterator<Loan> ita = it.next().getLoans().iterator(); it.hasNext();) {
					tempList.add(ita.next());
				}
			}
		}

		return tempList;
	}
	
	public Loan getLoan(Person person, int id)
	{
		Loan loan = new Loan();
		ArrayList<Loan> loans = getLoans(person);
		
		for(Iterator<Loan> it = loans.iterator(); it.hasNext();)
		{
			Loan temp = it.next();
			if(temp.getId() == id)
				loan = temp;
		}
		
		return loan;
	}

	public ArrayList<Person> getPeople() {
		return this.people;
	}
	
	public ArrayList<Person> getPeople(String partialName) {
		ArrayList<Person> people = new ArrayList<Person>();
		
		for (Iterator<Person> it = people.iterator(); it.hasNext();) {
			Person temp = it.next();
			if (temp.getName().contains(partialName))
				people.add(temp);
		}
		
		
		return people;
	}

	public Person getPerson(int id) {
		Person person = null;

		for (Iterator<Person> it = people.iterator(); it.hasNext();) {
			Person temp = it.next();
			if (temp.getId() == id)
				person = temp;
		}

		if (person != null)
			return person;
		else
			return null;
	}

	public Person getPerson(String name) {
		Person person = null;

		for (Iterator<Person> it = people.iterator(); it.hasNext();) {
			Person temp = it.next();
			if (temp.getName().equals(name))
				person = temp;
		}

		if (person != null)
			return person;
		else
			return null;
	}

	public Person getPerson(String address, int postCode, String city) {
		Person person = null;

		for (Iterator<Person> it = people.iterator(); it.hasNext();) {
			Person temp = it.next();
			if (temp.getAddress().equals(address)
					&& temp.getPostalCode() == postCode
					&& temp.getCity() == city)
				person = temp;
		}

		if (person != null)
			return person;
		else
			return null;
	}

	public void returnLoan(Person person, int loanID) {
		person.returnLoan(loanID);
	}
	
	public void removeDVD(DVD dvd) {
		dvds.remove(dvd);
	}
	
	public void removeDVD(String title) {
		removeDVD(getDVD(title));
	}
	
	public void removeCopy(Copy copy) {
		copies.remove(copy);
	}
	
	public void removeCopy(String serialNum) {
		removeCopy(getCopy(serialNum));
	}
	
	public void removePerson(Person person) {
		people.remove(person);
	}
	
	public void removePerson(String address, String city, int postCode) {
		removePerson(getPerson(address, postCode, city));
	}
	
	public void editPerson(Person person, String name, String address, String city, int postalCode, String phone)
	{
		person.setAddress(address);
		person.setCity(city);
		person.setName(name);
		person.setPhone(phone);
		person.setPostalCode(postalCode);
	}
	
	public void editDVD(DVD dvd, String title, String publishDate) {
		dvd.setTitle(title);
		dvd.setPublishDate(publishDate);
	}
	
	public void editCopy(Copy copy, String serialNum, String purchaseDate, double purchasePrice) {
		copy.setSerialNum(serialNum);
		copy.setPurchaseDate(purchaseDate);
		copy.setPurchasePrice(purchasePrice);
	}
	
	public void editLoan(Person person, int id, int period) {
		this.getLoan(person, id).setPeriod(period);
	}
}
